package com.example.tests;
import static com.example.tests.ContactDataGenerator.generateRandomContacts;
import static com.example.tests.GroupDataGenerator.generateRandomGroups;

import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;

import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.DataProvider;

import com.example.fw.AppManager;
import com.example.fw.RandomGenerator;

public class TestBase{

		protected static AppManager app;
		protected static RandomGenerator rnd;
		public ArrayList<Object[]> list;
		private int checkCounter;
		private int checkFrequency;
				
        @BeforeSuite
        public void setUp() throws Exception {
                String configFile = System.getProperty("configFile", "application.properties");
        		Properties properties = new Properties();
                properties.load(new FileReader(new File(configFile)));
        		app = new AppManager(properties);
                //rnd = new RandomGenerator(app);
        		checkCounter = 0;
        		checkFrequency = Integer.parseInt(properties.getProperty("check.frequency","0"));
        }

        public boolean wantToCheck() {
        	checkCounter++;
        	if (checkCounter > checkFrequency) {
        		checkCounter = 0;
        		return true;
        	} else {
        		return false;
        	}        	
        }
        @AfterSuite
        public void tearDown() throws Exception {
                app.stop();
        }
        
        @DataProvider
    	public Iterator<Object[]> randomValidGroupGenerator(){
    		 return wrapGroupsForDataProvider(generateRandomGroups(5)).iterator();
    	}
    	
    	public static ArrayList<Object[]> wrapGroupsForDataProvider(List<GroupData> groups) {
			ArrayList<Object[]> list = new ArrayList<Object[]>();
			for (GroupData group : groups){
				list.add(new Object[]{group});
			}
			return list;
		}
    	
		@DataProvider
    	public Iterator<Object[]> randomValidContactGenerator(){
    		return wrapContactsForDataProvider(generateRandomContacts(5)).iterator();
    	}
    	
    	public static ArrayList<Object[]> wrapContactsForDataProvider(List<ContactData> contacts) {
			ArrayList<Object[]> list = new ArrayList<Object[]>();
			for (ContactData contact : contacts){
				list.add(new Object[]{contact});
			}
			return list;
		}
}